//
//  AddressInputCell.m
//  PigMarket
//
//  Created by mac on 2019/3/25.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AddressInputCell.h"

@interface AddressInputCell()
/**
 title
 */
@property (nonatomic , strong) UILabel *titleLab;

/**
 竖线
 */
@property (nonatomic , strong) UIView *sLine;

/**
 横线
 */
@property (nonatomic , strong) UIView *hLine;
/**
 通讯录
 */
@property (nonatomic , strong) UIButton *addressbookBtn;



@end

@implementation AddressInputCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    AddressInputCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[AddressInputCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.sLine];
    [self.contentView addSubview:self.detailField];
    [self.contentView addSubview:self.addressbookBtn];
    [self.contentView addSubview:self.homeBtn];
    [self.contentView addSubview:self.schoolBtn];
    [self.contentView addSubview:self.companyBtn];
    [self.contentView addSubview:self.defaultAddressSwitch];
}

- (void)initLayout {
    // 布局子控件
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.top.mas_equalTo(18);
        make.bottom.mas_equalTo(-18);
    }];
    [self.sLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(98);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(1, 30));
    }];
    [self.detailField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.sLine.mas_right).offset(10);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(18);
        make.bottom.mas_equalTo(-18);
    }];
    [self.addressbookBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
    }];
    [self.homeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLab.mas_right).offset(22);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(58, 26));
    }];
    [self.schoolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.homeBtn.mas_right).offset(10);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(58, 26));
    }];
    [self.companyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.schoolBtn.mas_right).offset(10);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(58, 26));
    }];
    [self.defaultAddressSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-20);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(48, 26));
    }];
}

- (void)configCellWithInfoDic:(NSDictionary *)infoDic indexPath:(NSIndexPath *)indexPath {
    
    self.titleLab.text = infoDic[@"title"];
    if (indexPath.section == 0) {
        if ([infoDic[@"detail"] isBlankString]) {
            self.detailField.placeholder = infoDic[@"placeholder"];
        }else {
            self.detailField.text = infoDic[@"detail"];
        }
        
        if (indexPath.row == 0) {//显示通讯录
            [self.addressbookBtn setBackgroundImage:[UIImage imageNamed:@"user_address_addressbook"] forState:UIControlStateNormal];
        }else if (indexPath.row == 2) {//显示箭头
            self.detailField.enabled = NO;
            [self.addressbookBtn setBackgroundImage:[UIImage imageNamed:@"user_gt"] forState:UIControlStateNormal];
        }else {
            self.addressbookBtn.hidden = YES;
        }
        if (indexPath.row == 1) {
            self.detailField.keyboardType = UIKeyboardTypePhonePad;
        }
        self.homeBtn.hidden = YES;
        self.schoolBtn.hidden = YES;
        self.companyBtn.hidden = YES;
        self.defaultAddressSwitch.hidden = YES;
    }else if (indexPath.section == 1) {
        if ([infoDic[@"detail"] isEqualToString:@"家"]) {
            self.homeBtn.selected = YES;
            self.homeBtn.layer.borderWidth = 1;
            self.homeBtn.layer.borderColor = [UIColor primaryColor].CGColor;

        }else if ([infoDic[@"detail"] isEqualToString:@"学校"]){
            self.schoolBtn.selected = YES;
            self.schoolBtn.layer.borderWidth = 1;
            self.schoolBtn.layer.borderColor = [UIColor primaryColor].CGColor;

        }else if ([infoDic[@"detail"] isEqualToString:@"公司"]){
            self.companyBtn.selected = YES;
            self.companyBtn.layer.borderWidth = 1;
            self.companyBtn.layer.borderColor = [UIColor primaryColor].CGColor;
        }
        
        self.sLine.hidden = YES;
        self.detailField.hidden = YES;
        self.defaultAddressSwitch.hidden = YES;
    }else {
        if ([infoDic[@"detail"] intValue] == 0) {
            self.defaultAddressSwitch.on = NO;
        }else if ([infoDic[@"detail"] intValue] == 1){
            self.defaultAddressSwitch.on = YES;
        }else {
            self.defaultAddressSwitch.on = NO;
        }
        self.sLine.hidden = YES;
        self.detailField.hidden = YES;
        self.homeBtn.hidden = YES;
        self.schoolBtn.hidden = YES;
        self.companyBtn.hidden = YES;
    }
}

//获取通讯录事件
- (void)addressBookTapped {
    if (self.addressBookBlock) {
        self.addressBookBlock();
    }
}
//标签按钮单选事件
- (void)selectLabTapped:(UIButton *)button {
    button.selected = !button.selected;

    NSMutableArray *labBtnArr = [[NSMutableArray alloc] initWithObjects:self.homeBtn,self.schoolBtn,self.companyBtn, nil];
    for (int i = 0; i < 3; i++) {
        UIButton *btn = labBtnArr[i];
        if (button.tag == 100+i) {
            btn.selected = button.selected;
        }else {
            btn.selected = NO;
        }
        [btn setTitleColor:[UIColor colorWithHexString:@"#454545"] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithHexString:@"#D8D8D8"].CGColor;
    }
    
    UIButton *selectBtn = labBtnArr[button.tag - 100];
    if (selectBtn.selected) {
        [selectBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        selectBtn.layer.borderColor = [UIColor primaryColor].CGColor;
    }
}
- (void)defaultAddressTapped {
    self.defaultAddressSwitch.on = !self.defaultAddressSwitch.on;
}
#pragma mark - getter and setter

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor textBlackColor];
        _titleLab.font = [UIFont largerFont];
    }
    return _titleLab;
}

- (UIView *)sLine {
    if (!_sLine) {
        _sLine = [[UIView alloc] init];
        _sLine.backgroundColor = [UIColor colorWithHexString:@"#DFDFDF"];
    }
    return _sLine;
}
- (UITextField *)detailField {
    if (!_detailField) {
        _detailField = [[UITextField alloc] init];
        _detailField.textColor = [UIColor textBlackColor];
        _detailField.font = [UIFont largerFont];
    }
    return _detailField;
}
- (UIButton *)addressbookBtn {
    if (!_addressbookBtn) {
        _addressbookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addressbookBtn addTarget:self action:@selector(addressBookTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addressbookBtn;
}
- (UIButton *)homeBtn {
    if (!_homeBtn) {
        _homeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_homeBtn setTitle:@"家" forState:UIControlStateNormal];
        [_homeBtn setTitleColor:[UIColor colorWithHexString:@"#454545"] forState:UIControlStateNormal];
        [_homeBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateSelected];
        [_homeBtn addTarget:self action:@selector(selectLabTapped:) forControlEvents:UIControlEventTouchUpInside];
        _homeBtn.titleLabel.font = [UIFont smallFont];
        _homeBtn.layer.borderWidth = 1;
        _homeBtn.layer.borderColor = [UIColor colorWithHexString:@"#D8D8D8"].CGColor;
        _homeBtn.layer.cornerRadius = 3;
        _homeBtn.layer.masksToBounds = YES;
        _homeBtn.tag = 100;
    }
    return _homeBtn;
}
- (UIButton *)schoolBtn {
    if (!_schoolBtn) {
        _schoolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_schoolBtn setTitle:@"学校" forState:UIControlStateNormal];
        [_schoolBtn setTitleColor:[UIColor colorWithHexString:@"#454545"] forState:UIControlStateNormal];
        [_schoolBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateSelected];
        [_schoolBtn addTarget:self action:@selector(selectLabTapped:) forControlEvents:UIControlEventTouchUpInside];
        _schoolBtn.titleLabel.font = [UIFont smallFont];
        _schoolBtn.layer.borderWidth = 1;
        _schoolBtn.layer.borderColor = [UIColor colorWithHexString:@"#D8D8D8"].CGColor;
        _schoolBtn.layer.cornerRadius = 3;
        _schoolBtn.layer.masksToBounds = YES;
        _schoolBtn.tag = 101;
    }
    return _schoolBtn;
}
- (UIButton *)companyBtn {
    if (!_companyBtn) {
        _companyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_companyBtn setTitle:@"公司" forState:UIControlStateNormal];
        [_companyBtn setTitleColor:[UIColor colorWithHexString:@"#454545"] forState:UIControlStateNormal];
        [_companyBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateSelected];
        [_companyBtn addTarget:self action:@selector(selectLabTapped:) forControlEvents:UIControlEventTouchUpInside];

        _companyBtn.titleLabel.font = [UIFont smallFont];
        _companyBtn.layer.borderWidth = 1;
        _companyBtn.layer.borderColor = [UIColor colorWithHexString:@"#D8D8D8"].CGColor;
        _companyBtn.layer.cornerRadius = 3;
        _companyBtn.layer.masksToBounds = YES;
        _companyBtn.tag = 102;
    }
    return _companyBtn;
}
- (UISwitch *)defaultAddressSwitch {
    if (!_defaultAddressSwitch) {
        _defaultAddressSwitch = [[UISwitch alloc] init];
        _defaultAddressSwitch.tintColor = [UIColor colorWithHexString:@"#DFDFDF"];
        _defaultAddressSwitch.onTintColor = [UIColor primaryColor];
        _defaultAddressSwitch.on = NO;
        [_defaultAddressSwitch addTarget:self action:@selector(defaultAddressTapped) forControlEvents:(UIControlEventValueChanged)];
    }
    return _defaultAddressSwitch;
}

@end
